package any;

import java.sql.Statement;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SQLiteDemo {
	public static void main(String[] args) {
		try {
			String dbPath = "D:/sqliteDemo.db";
			File db = new File(dbPath);
			if(!db.exists())
				db.createNewFile();
			
			Class.forName("org.sqlite.JDBC");
			Connection connection = null;
			
			//Use File Databases
			//connection = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
			
			//Use Memory Databases
			connection = DriverManager.getConnection("jdbc:sqlite::memory:");
			Statement statement = connection.createStatement();
			statement.setQueryTimeout(20);
			statement.executeUpdate("drop table if exists person");
		    statement.executeUpdate("create table person (id integer, name string)");
		    statement.executeUpdate("insert into person values(1, 'leo')");
		    statement.executeUpdate("insert into person values(2, 'yui')");
		    ResultSet rs = statement.executeQuery("select * from person");
		    while(rs.next())
		    {
		    	// read the result set
		    	System.out.println("name = " + rs.getString("name"));
		    	System.out.println("id = " + rs.getInt("id"));
		    }
			connection.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}
